package com.tucc.hello.leetcode.no1_400.no1_200.no21_40.no25;

import com.tucc.hello.leetcode.common.ListNode;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode listNode = ListNode.of(1, 2, 3, 4, 5);
        ListNode newHead = solution.reverseKGroup(listNode, 3);
        ListNode.print(newHead);
    }

    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode[] kNode = new ListNode[k];
        ListNode temp = head;
        int i = 0;
        for(;i < k && temp != null;i ++){
            kNode[i] = temp;
            temp = temp.next;
        }
        if(i < k){
            return head;
        }
        head.next = reverseKGroup(kNode[k - 1].next, k);
        for(i = 1;i < k;i ++){
            kNode[i].next = kNode[i - 1];
        }
        return kNode[k - 1];
    }


}
